<?

// Do a mysql query
function doQuery($query)
{
  $result = @mysql_query($query);

	if (!$result)
	{
  	exit('<p>There was an error performing the query. Additional details: ' . mysql_error() . '</p>');
	}

  elseif (mysql_num_rows($result) == 0)
  {
	  exit("<p>No results were returned for your query.</p>");
	}
  else
  {
	  return($result);
	}
}

//Overall function to calculate points
function  calc_points($score1,$score2,$size1,$size2)
{
 $points =  ((100 * score_proportion($score1,$score2)) * team_weighting($size1,$size2));
 return ($points);
}

//This version doesn't use team weightings
function  calc_points2($score1,$score2)
{
 $points =  (100 * score_proportion($score1,$score2));
 return ($points);
}


//Work out the weighting. Used to adjust points if team sizes are not equal
function  team_weighting($wsize1,$wsize2)
{
 $weighting = (((((($wsize2 / $wsize1) * ($wsize2 / $wsize1)) -1)) /2) +1);
 //echo ("Weighting: $weighting<br />");
 return ($weighting);

}

//Not sure what this does!
function goal_tightening($tscore1,$tscore2)
{
 if ($tscore1 == $tscore2)
 {
  return (0);
 }

 $total_goals = $tscore1 + $tscore2;
 $goal_diff = make_pos ($tscore1 - $tscore2);

 $tightness = make_pos ((($tscore1 / $total_goals) - ($tscore2 / $total_goals))) / (3 * $goal_diff);
 //echo ("Tightness: $tightness<br />");
 return ($tightness);
}

//Again, ummm?
function score_proportion($pscore1,$pscore2)
{
  
 if ($pscore1 == $pscore2)
 {
  return (0.5);
 }
 
 $total_goals = $pscore1 + $pscore2;
 
 if (($pscore1 / $total_goals) > 0.5)
 {
    $prop= ($pscore1 / $total_goals) - goal_tightening($pscore1, $pscore2);
 }
 else
 {
     $prop= ($pscore1 / $total_goals) + goal_tightening($pscore1, $pscore2);
 }
 return ($prop);
}

//There is probably a better way of doing this!
function make_pos($number)
{
 if ($number > 0)
 {
  return $number;
 }
 else
 {
  return 0 - $number;
 }

}

function displayMatchResults($sub_date,$match_type)
{
print ("<div id=page><div id=content><div class=post><div class=title><h2><a href=#>Current Scoreboard($match_type)</a></h2><p><small>Click table headers to sort</small></p></div></div></div>");

$match_sql = "SELECT date,
                     score1,score2,team1,team2,
                     TRUNCATE(points1,2) as team1_points,
                     TRUNCATE(points2,2) as team2_points
                     FROM games WHERE date <= '$sub_date'
                     AND matchtype = '$match_type'
                     ORDER BY date ASC";

        $match_result = doQuery($match_sql);

        $run = 0;

        while ($row = mysql_fetch_array($match_result))
		 		{
              $game_date = $row['date'];
              $game_score1 = $row['score1'];
              $game_score2 = $row['score2'];
              $game_team1 = $row['team1'];
              $game_team2 = $row['team2'];
              $game_points1 = $row['team1_points'];
              $game_points2 = $row['team2_points'];

        $team1 = explode (",",$game_team1);
        $team2 = explode (",",$game_team2);

        $team1_win = 0;
        $team1_draw = 0;
        $team1_lose = 0;

        $team2_win = 0;
        $team2_draw = 0;
        $team2_lose = 0;

        if (count($current_rank) != 0)
        {
           foreach ($current_rank as $player=>$c_rank)
           {
                $previous_rank[$player] = $c_rank;
                $current_rank[$player] = 0;
           }
        }


        if ($game_score1 > $game_score2)
        {
           $team1_win = 1;
           $team2_lose = 1;
        }
        elseif ($game_score1 == $game_score2)
        {
           $team1_draw = 1;
           $team2_draw = 1;
        }
        else
        {
            $team1_lose = 1;
            $team2_win = 1;
        }



        foreach ($team1 as $player1)
        {
        $points_temp = $points[$player1];
        $points_temp += $game_points1;
        $points[$player1] = $points_temp;

        $wins_temp = $wins[$player1];
        $wins_temp += $team1_win;
        $wins[$player1] = $wins_temp;

        $draws_temp = $draws[$player1];
        $draws_temp += $team1_draw;
        $draws[$player1] = $draws_temp;

        $loses_temp = $loses[$player1];
        $loses_temp += $team1_lose;
        $loses[$player1] = $loses_temp;

        $played_temp = $played[$player1];
        $played_temp++;
        $played[$player1] = $played_temp;

        }



        foreach ($team2 as $player2)
        {
        $points_temp =  $points[$player2];
        $points_temp += $game_points2;
        $points[$player2] = $points_temp;

        $wins_temp = $wins[$player2];
        $wins_temp += $team2_win;
        $wins[$player2] = $wins_temp;

        $draws_temp = $draws[$player2];
        $draws_temp += $team2_draw;
        $draws[$player2] = $draws_temp;

        $loses_temp = $loses[$player2];
        $loses_temp += $team2_lose;
        $loses[$player2] = $loses_temp;

        $played_temp = $played[$player2];
        $played_temp++;
        $played[$player2] = $played_temp;
      }
        foreach($points as $player=>$score)
        {
            $average_points[$player] = $score / $played[$player];
        }

        arsort($average_points);
        $rank = 1;
        foreach ($average_points as $player=>$average)
        {
                $current_rank[$player] = $rank;
                $rank++;
        }


      }




      print("<table BORDER cellpadding=5 class=sortable>");
      print("<tr><th>Name</th><th>Total</th><th>Average</th><th>Played</th><th>Won</th><th>Drawn</th><th>Lost</th><th>Position</th></tr>");



      foreach($average_points as $player=>$average)
      {
         $formatted_average = sprintf("%01.2f", $average);
         print ("<tr><td>$player</td><td>$points[$player]</td><td>$formatted_average</td><td>$played[$player]</td><td>$wins[$player]</td><td>$draws[$player]</td><td>$loses[$player]</td>");

         if ($previous_rank[$player] != "")
         {
            if ($current_rank[$player] < $previous_rank[$player])
            {
               $difference = $previous_rank[$player] - $current_rank[$player];
               print ("<td><img src=/fletag/images/up.jpg>(+$difference)</td>");
            }
            elseif ($current_rank[$player] > $previous_rank[$player])
            {
               $difference = $current_rank[$player] - $previous_rank[$player];
               print ("<td><img src=/fletag/images/down.png>(-$difference)</td>");
            }
            elseif ($current_rank[$player] == $previous_rank[$player])
            {
               print ("<td><img src=/fletag/images/same.png></td>");
            }

         }
         else
         {
          print ("<td><img src=/fletag/images/new.jpg></td>");
         }

         print ("</tr>");



      }

					print("</table>");
}

function displayMatchResults2($sub_date,$match_type)
{
print ("<div id=page><div id=content><div class=post><div class=title><h2><a href=#>Current Scoreboard($match_type)</a></h2><p><small>Click table headers to sort</small></p></div></div></div>");

$match_sql = "SELECT date,
                     score1,score2,team1,team2,
                     TRUNCATE(points1,2) as team1_points,
                     TRUNCATE(points2,2) as team2_points
                     FROM games WHERE date <= '$sub_date'
                     AND matchtype = '$match_type'
                     ORDER BY date ASC";

        $match_result = doQuery($match_sql);

        $run = 0;

        while ($row = mysql_fetch_array($match_result))
		 		{
              $game_date = $row['date'];
              $game_score1 = $row['score1'];
              $game_score2 = $row['score2'];
              $game_team1 = $row['team1'];
              $game_team2 = $row['team2'];
              //$game_points1 = $row['team1_points'];
              //$game_points2 = $row['team2_points'];
              $game_points1 = sprintf('%.2f',calc_points2($game_score1,$game_score2));
              $game_points2 = sprintf('%.2f',calc_points2($game_score2,$game_score1));

        $team1 = explode (",",$game_team1);
        $team2 = explode (",",$game_team2);

        $team1_win = 0;
        $team1_draw = 0;
        $team1_lose = 0;

        $team2_win = 0;
        $team2_draw = 0;
        $team2_lose = 0;

        if (count($current_rank) != 0)
        {
           foreach ($current_rank as $player=>$c_rank)
           {
                $previous_rank[$player] = $c_rank;
                $current_rank[$player] = 0;
           }
        }


        if ($game_score1 > $game_score2)
        {
           $team1_win = 1;
           $team2_lose = 1;
        }
        elseif ($game_score1 == $game_score2)
        {
           $team1_draw = 1;
           $team2_draw = 1;
        }
        else
        {
            $team1_lose = 1;
            $team2_win = 1;
        }



        foreach ($team1 as $player1)
        {
        $points_temp = $points[$player1];
        $points_temp += $game_points1;
        $points[$player1] = $points_temp;

        $wins_temp = $wins[$player1];
        $wins_temp += $team1_win;
        $wins[$player1] = $wins_temp;

        $draws_temp = $draws[$player1];
        $draws_temp += $team1_draw;
        $draws[$player1] = $draws_temp;

        $loses_temp = $loses[$player1];
        $loses_temp += $team1_lose;
        $loses[$player1] = $loses_temp;

        $played_temp = $played[$player1];
        $played_temp++;
        $played[$player1] = $played_temp;

        }



        foreach ($team2 as $player2)
        {
        $points_temp =  $points[$player2];
        $points_temp += $game_points2;
        $points[$player2] = $points_temp;

        $wins_temp = $wins[$player2];
        $wins_temp += $team2_win;
        $wins[$player2] = $wins_temp;

        $draws_temp = $draws[$player2];
        $draws_temp += $team2_draw;
        $draws[$player2] = $draws_temp;

        $loses_temp = $loses[$player2];
        $loses_temp += $team2_lose;
        $loses[$player2] = $loses_temp;

        $played_temp = $played[$player2];
        $played_temp++;
        $played[$player2] = $played_temp;
      }
        foreach($points as $player=>$score)
        {
            $average_points[$player] = $score / $played[$player];
        }

        arsort($average_points);
        $rank = 1;
        foreach ($average_points as $player=>$average)
        {
                $current_rank[$player] = $rank;
                $rank++;
        }


      }




      print("<table BORDER cellpadding=5 class=sortable>");
      print("<tr><th>Name</th><th>Total</th><th>Average</th><th>Played</th><th>Won</th><th>Drawn</th><th>Lost</th><th>Position</th></tr>");



      foreach($average_points as $player=>$average)
      {
         $formatted_average = sprintf("%01.2f", $average);
         print ("<tr><td>$player</td><td>$points[$player]</td><td>$formatted_average</td><td>$played[$player]</td><td>$wins[$player]</td><td>$draws[$player]</td><td>$loses[$player]</td>");

         if ($previous_rank[$player] != "")
         {
            if ($current_rank[$player] < $previous_rank[$player])
            {
               $difference = $previous_rank[$player] - $current_rank[$player];
               print ("<td><img src=/fletag/images/up.jpg>(+$difference)</td>");
            }
            elseif ($current_rank[$player] > $previous_rank[$player])
            {
               $difference = $current_rank[$player] - $previous_rank[$player];
               print ("<td><img src=/fletag/images/down.png>(-$difference)</td>");
            }
            elseif ($current_rank[$player] == $previous_rank[$player])
            {
               print ("<td><img src=/fletag/images/same.png></td>");
            }

         }
         else
         {
          print ("<td><img src=/fletag/images/new.jpg></td>");
         }

         print ("</tr>");



      }

					print("</table>");
}


?>
